package com.qiantu.phone.ui.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.qiantu.api.db.LLHouseDBManager;
import com.qiantu.api.entity.HouseBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;

import java.util.List;

/**
 * @Description: 房屋列表

 * @return:
 * @Author:
 * @Time:  2022年12月22日
 */
public class OwningHouseActivity extends AppActivity{

    private RecyclerView rvHouse;
    private HouseAdapter houseAdapter;
    private List<HouseBean> houses;
    private HouseBean curHouseInfo;
    private int subscript = -1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_owning_house;
    }

    @Override
    protected void initView() {
        rvHouse = (RecyclerView) findViewById(R.id.rv_house);
        houseAdapter=new HouseAdapter();
        rvHouse.setLayoutManager(new LinearLayoutManager(this));
        rvHouse.setAdapter(houseAdapter);
        houseAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                subscript = position;
                houseAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取本地房屋列表
    private void getData() {
        houses = LLHouseDBManager.getInstance(this).getAll();
        if(houses == null || houses.size() == 0){
            return;
        }
        curHouseInfo = AppApplication.getInstance().getCurHouseInfo();
        if (curHouseInfo != null) {
            //从本地数据拿下最新的数据，有可能房屋被别人删除获取修改，后台进前台刷新了本地数据库
            curHouseInfo = LLHouseDBManager.getInstance(this).getHouseByHouseSerialNo(curHouseInfo.getHouseSerialNo());
            if(curHouseInfo == null){
                curHouseInfo = houses.get(0);
            }
        } else {
            curHouseInfo = houses.get(0);
        }
        if (curHouseInfo != null) {
        }
        houseAdapter.setNewInstance(removeDefaultHouse(houses));
    }


    private class HouseAdapter extends BaseQuickAdapter<HouseBean, BaseViewHolder> {

        public HouseAdapter() {
            super(R.layout.house_item2);
        }

        @Override
        protected void convert(BaseViewHolder helper, HouseBean item) {
            helper.setText(R.id.home_name, item.getName());
            helper.setText(R.id.home_location, item.getAddress());
            helper.setText(R.id.roleType, getRoleType(item.getRoleType()));
            CheckBox checkbox_house = helper.getView(R.id.checkbox_house);
            checkbox_house.setChecked(subscript == helper.getAdapterPosition());
        }
    }


    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        Intent intent = new Intent(this, BindingDevicesActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("houseSerialNo",houses.get(subscript).getHouseSerialNo());
        intent.putExtra("house_name",houses.get(subscript).getName());
        startActivity(intent);
    }

    private String getRoleType(int type){
        String result = getString(R.string.huzhu);
        if (type == 0){
            result = getString(R.string.huzhu);
        }else if (type == 1){
            result = getString(R.string.guanliyuan);
        }else if(type == 2){
            result = "普通成员";
        }
        return result;
    }

    public List<HouseBean> removeDefaultHouse(List<HouseBean> data) {
        if (data == null || data.isEmpty()) return null;
        if (curHouseInfo == null || TextUtils.isEmpty(curHouseInfo.getHouseSerialNo())) {
            return data;
        }
        for (HouseBean bean : data) {
            if (curHouseInfo.getHouseSerialNo().equals(bean.getHouseSerialNo())) {
                return data;
            }
        }
        return null;
    }
}